

hrds = read_mrt('./ancillary/HRDS_catalog.mrt')

openw,lun,'/d1/BGPS/bgps_svn/papers/13co_distances/tables/hrds.tex',/get_lun



printf,lun,'%%=================================%%'
printf,lun,'%%                                 %%'
printf,lun,'%%   Table: HII Regions and KDARs  %%'
printf,lun,'%%   Label: table:hrds             %%'
printf,lun,'%%                                 %%'
printf,lun,'%%=================================%%'
printf,lun,'\begin{deluxetable*}{lccccccc}'
printf,lun,'  \tablecolumns{8}'
printf,lun,'  \tablewidth{0pc}'
printf,lun,'  \tabletypesize{\footnotesize}'
printf,lun,'  \tablecaption{\HII\ Region KDA Resolutions from the HRDS\label{table:hrds}}'
printf,lun,'  \tablehead{'
printf,lun,'    \colhead{Object}  & \colhead{$\ell$} & \colhead{$b$} & \colhead{\vlsr} & \colhead{\dsun} & \colhead{KDA\tablenotemark{a}} & \colhead{QF\tablenotemark{b}} & \colhead{Ref.} \\'
printf,lun,'    \colhead{Name} & \colhead{(\degr)} & \colhead{(\degr)} & \colhead{(\kms)} & \colhead{(kpc)} & \colhead{Resol.} & \colhead{} & \colhead{}'
printf,lun,'  }'
printf,lun,'  \startdata'

tab1 = where(hrds.source EQ 0, n1) ; Pick 6
tab2 = where(hrds.source EQ 1, n2) ; Pick 3
tab3 = where(hrds.source EQ 2, n3) ; Pick 1


print,hrds[tab3].kdar

ind1 = random_independent_integers(6,n1)
ind2 = random_independent_integers(3,n2)
ind3 = random_independent_integers(1,n3)


inds = [tab1[ind1],tab2[ind2],tab3[ind3]]
inds = inds[sort(inds)]

sample = hrds[inds]

FOR ii=0,n_elements(sample)-1 DO BEGIN
   
   h = sample[ii]
   
   d = finite(h.dsun) ? (h.dsun LT 10. ? '\phn' : '') + $
       string(h.dsun,format="(F0.2)") : $
       '\nodata'
   z = finite(h.z) ? '$'+string(round(h.z),format="(I0)")+'$' : '\nodata'
   qf = strtrim(h.qf,2) NE '' ? h.qf : '\nodata'
   print,h.qf EQ ''
   
   velphantom = ''
   IF h.vlsr GE 0 THEN velphantom += '\phs'
   IF abs(h.vlsr) LE 100. THEN velphantom += '\phn'
   IF abs(h.vlsr) LE 10. THEN velphantom += '\phn'
   
   IF h.vlsr GT 100 THEN print,'Bigger than 100'
   IF h.vlsr LT 0 THEN print,'Negative'
   
   str = string(h.name,h.glon,h.glat GE 0 ? '\phs' : '',$
                h.glat,velphantom,$
                h.vlsr,d,$
                h.kdar,qf,h.source+1,format="(A0,' & $',F0.3,'$ & ',A0,'$',"+$
                "F0.3,'$ & ',A0,'$',F0.1,'$ & ',A0,' & ',"+$
                "A0,' & ',A0,' & ',I0)")
   
   str += ' \\'
   printf,lun,str
   
ENDFOR

printf,lun,'  \enddata'
printf,lun,'  \tablerefs{1: \citet{Anderson:2009a}; 2. \citet{Anderson:2012}, 3. \citet{Bania:2012}}'
printf,lun,'  \tablenotetext{a}{N = near, F = far, T = tangent}'
printf,lun,'  \tablenotetext{b}{Quality Factor of the KDA resolution; see \citet{Anderson:2012} for more details.  \citet{Bania:2012} does not assign a QF.}'
printf,lun,'  \tablecomments{This table is available in its entirety in a machine-readable format in the online journal.  A portion is shown here for guidance regarding its form and content.}'
printf,lun,'\end{deluxetable*}'
printf,lun,'%%=================================%%'


close,lun
free_lun,lun





END
